﻿using System;
using System.Collections.Generic;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using KPIS.DBM;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_Warehouse
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        public IList<MasterWarehouseInfo> GetListMasterWarehouse()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterWarehouseInfo> infoList = new List<MasterWarehouseInfo>();
            try
            {
                dbManager.Open();

                strSQL = " SELECT icm_wh_seq,icm_wh_name,icm_wh_descr "
                         + " FROM icm_mas_warehouse "
                         + " WHERE record_status <> 'D' ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterWarehouseInfo info = new MasterWarehouseInfo();
                        info.Seq = Convert.ToInt32(dr[i]["icm_wh_seq"]);
                        info.Name = (dr[i]["icm_wh_name"]).ToString();
                        info.Descr = (dr[i]["icm_wh_descr"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }


        public MasterWarehouseInfo GetListFillBySeq(Nullable<Int32> icm_wh_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            MasterWarehouseInfo info = new MasterWarehouseInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_wh_seq", icm_wh_seq);

                strSQL = " SELECT icm_wh_seq,icm_wh_name,icm_wh_descr "
                         + " FROM icm_mas_warehouse "
                         + " WHERE record_status <> 'D' and icm_wh_seq = @icm_wh_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.Seq = Convert.ToInt32(drc[0]["icm_wh_seq"]);
                    info.Name = drc[0]["icm_wh_name"].ToString();
                    info.Descr = drc[0]["icm_wh_descr"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<MasterWarehouseInfo> GetListFillByName(string icm_wh_name)
        {
            string strSQL = "";
            DataRowCollection drc = null;
            IList<MasterWarehouseInfo> infoList = new List<MasterWarehouseInfo>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_wh_seq,icm_wh_name,icm_wh_descr "
                         + " FROM icm_mas_warehouse "
                         + " WHERE record_status <> 'D' and icm_wh_name LIKE '%' '" + icm_wh_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        MasterWarehouseInfo info = new MasterWarehouseInfo();
                        info.Seq = Convert.ToInt32(drc[i]["icm_wh_seq"]);
                        info.Name = drc[i]["icm_wh_name"].ToString();
                        info.Descr = drc[i]["icm_wh_descr"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }
    }
}
